<?php

###########################################################################
#   Copyright (C) 2012 CDNPAL LTD by Christopher Gu                       #
#   resources@cdnpal.com                                                  #
#                                                                         #
#   coded for Php 4.3/5.X and above                                       #
#   and AllowOverride all for .htaccess                                   #
#                                                                         #
#   This program is free software released                                #
#   under the GPL version 2 license:                                      #
#   http://www.gnu.org/licenses/gpl-2.0.htm                               #
#                                                                         #
#   This program is distributed WITHOUT ANY WARRANTY                      #
#   or FITNESS FOR A PARTICULAR PURPOSE.                                  #
#                                                                         #
#   DADF-SDK                                                              #
#                                                                         #
###########################################################################

class DatabaseController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	
    }

    
	public function getAction()
    {
    	$this->_helper->viewRenderer->setNoRender(true);
    	
    	/*
    	 * GET various types of stored DADF documents
    	 * 
    	 * for now this will just get documents stored in the database
    	 * and output them as XML but in the future that will only happen
    	 * with the automated business logic as it sends out 
    	 * request bids for new offers
    	 */
    	
       	$request = $this->getRequest();
    
       	$param_get_id 		= $request->getParam('getid');
       	$param_direction 	= $request->getParam('type');
       	
		$direction 	= !empty($param_direction) 	? $param_direction 	: 'Incoming';
		
		if (empty($param_get_id)):
			die("No Database ID");	
		endif;
		
		$id 		= !empty($param_get_id) 	? $param_get_id 	: null;

		$action = $request->getParam('id');

    	switch ($action):
    	
    		case "dadfrequest":
    			Dadf2XMLService::convert_bid_request($id, $direction);
    			break;
       		
    		case "dadfoffer":
				Dadf2XMLService::convert_offer_response($id, $direction);
    			break;
         	
       		case "dadfaccept":
				Dadf2XMLService::convert_accept_response($id, $direction);	
         		break;
         		
       		case "dadfaccepted":
				Dadf2XMLService::convert_accepted_response($id, $direction);	
         		break;
         		
         	default:

         		break;
    	
    	endswitch;
		
    }
    
    public function indexAction()
    {

    	$params = array();
    	
		$incoming_bid_request_factory = IncomingBidRequestFactory::get_instance();
		$outgoing_bid_request_factory = OutgoingBidRequestFactory::get_instance();
    	
		$this->view->assign("incoming_bid_requests", $incoming_bid_request_factory->get($params));
		$this->view->assign("outgoing_bid_requests", $outgoing_bid_request_factory->get($params));
		
		$incoming_offer_response_factory = IncomingOfferResponseFactory::get_instance();
		$outgoing_offer_response_factory = OutgoingOfferResponseFactory::get_instance();
		
		$this->view->assign("incoming_offer_responses", $incoming_offer_response_factory->get($params));
		$this->view->assign("outgoing_offer_responses", $outgoing_offer_response_factory->get($params));
		
		$incoming_accept_response_factory = IncomingAcceptResponseFactory::get_instance();
		$outgoing_accept_response_factory = OutgoingAcceptResponseFactory::get_instance();

    	$this->view->assign("incoming_accept_responses", $incoming_accept_response_factory->get($params));
		$this->view->assign("outgoing_accept_responses", $outgoing_accept_response_factory->get($params));
		
		$incoming_accepted_response_factory = IncomingAcceptedResponseFactory::get_instance();
		$outgoing_accepted_response_factory = OutgoingAcceptedResponseFactory::get_instance();

    	$this->view->assign("incoming_accepted_responses", $incoming_accepted_response_factory->get($params));
		$this->view->assign("outgoing_accepted_responses", $outgoing_accepted_response_factory->get($params));
		
    }
}

